// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CookieProtectionHelperWrapper.cs" company="Anton Frattaroli">
//   Copyright (c) Adam Tibi. All rights reserved.
// </copyright>
// <summary>
//   Defines the CookieProtectionHelperWrapper class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine
{
    using System;
    using System.Reflection;
    using System.Web;
    using System.Web.Security;
    
    /// <summary>
    /// A wrapper arroud System.Web.Security.CookieProtectionHelper internal class
    /// </summary>
    public static class CookieProtectionHelperWrapper
    {
        /// <summary>
        /// Stores the Encode method.
        /// </summary>
        private static MethodInfo encode = typeof(HttpContext).Assembly.GetType("System.Web.Security.CookieProtectionHelper").GetMethod("Encode", BindingFlags.NonPublic | BindingFlags.Static);

        /// <summary>
        /// Stores the Decode method.
        /// </summary>
        private static MethodInfo decode = typeof(HttpContext).Assembly.GetType("System.Web.Security.CookieProtectionHelper").GetMethod("Decode", BindingFlags.NonPublic | BindingFlags.Static);

        /// <summary>
        /// Wrapper arround CookieProtectionHelper.Encode
        /// </summary>
        /// <param name="cookieProtection">Protection Type</param>
        /// <param name="buffer">Bytes buffer to encode</param>
        /// <param name="count">The number of bytes in the buffer</param>
        /// <returns>Encoded text</returns>
        public static string Encode(CookieProtection cookieProtection, byte[] buffer, int count)
        {
            return (string)encode.Invoke(null, new object[] { cookieProtection, buffer, count });
        }

        /// <summary>
        /// Wrapper arround CookieProtectionHelper.Decode
        /// </summary>
        /// <param name="cookieProtection">Protection Type</param>
        /// <param name="data">String to decode</param>
        /// <returns>Decoded bytes</returns>
        public static byte[] Decode(CookieProtection cookieProtection, string data)
        {
            return (byte[])decode.Invoke(null, new object[] { cookieProtection, data });
        }
    }
}